package com.glory.dgcpcpda.activity.cpc;

import android.content.Intent;
import android.util.Log;
import android.widget.LinearLayout;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

import butterknife.BindView;
import es.voghdev.pdfviewpager.library.PDFViewPager;
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter;

public class PdfViewActivity extends BaseActivity {
    @BindView(R.id.llMain)
    LinearLayout llMain;

    // 属性参数
    private String url;
    private Boolean isDelete;

    PDFViewPager pdfViewPager;
    PDFPagerAdapter adapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pdfview;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);

        pdfViewPager = findViewById(R.id.pdfViewPager);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        isDelete = intent.getBooleanExtra("isDelete", false);

        if (StringUtils.isNoneEmpty(url)){
            adapter = new PDFPagerAdapter.Builder(this)
                    .setErrorHandler(t -> {
                        String stackTraceString = Log.getStackTraceString(t);
                        Log.e("Error", stackTraceString);
                    })
                    .setPdfPath(url)
                    .create();

            pdfViewPager.setAdapter(adapter);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        adapter.close();

        if(isDelete){
            File file = new File(url);
            file.delete();
        }
    }
}
